package com.codi.bus.core.dao.impl;

import com.codi.base.dao.BaseDAOImpl;
import com.codi.base.dao.plugin.page.PageView;
import com.codi.base.util.ListUtil;
import com.codi.base.util.MapUtils;
import com.codi.bus.core.dao.OrgFileDao;
import com.codi.bus.core.domain.OrgFile;
import com.codi.bus.core.resultModel.OrganFileResult;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzhenhao on 2017/9/8.
 */
@Repository("orgFileDao")
public class OrgFileDaoImpl extends BaseDAOImpl<OrgFile> implements OrgFileDao{

    @Override
    public int deleteByPrimaryKey(Long id) {
        return this.delete(generateStatement("deleteByPrimaryKey"), id);
    }

    @Override
    public int deleteByUniqueFileName(String uniqueFileName) {
        return this.delete(generateStatement("deleteByUniqueFileName"), uniqueFileName);
    }

    @Override
    public int insertSelective(OrgFile record) {
        return this.insert(generateStatement("insertSelective"), record);
    }

    @Override
    public OrgFile selectByPrimaryKey(Long id) {
        return this.getObject(generateStatement("selectByPrimaryKey"), id);
    }

    @Override
    public List<OrganFileResult> queryFiles(Integer pageIndex, Integer pageSize) {
        PageView pageView = this.getPageView(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageView", pageView);
        List<Map<String, Object>> temp = getSqlSession().selectList(generateStatement("queryFiles"), map);

        List<OrganFileResult> result = this.setResult(temp);

        return result;
    }

    @Override
    public List<OrganFileResult> queryFilesBySearchContent(Integer pageIndex, Integer pageSize, String searchContent) {
        return null;
    }

    @Override
    public List<OrganFileResult> queryFilesByUserId(Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        List<Map<String, Object>> temp = getSqlSession().selectList(generateStatement("queryFilesByUserId"), map);

        List<OrganFileResult> result = this.setResult(temp);

        return result;
    }

    @Override
    public List<OrganFileResult> queryFilesByUserIdAndProfileId(Long userId, Long profileId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("profileId", profileId);
        List<Map<String, Object>> temp = getSqlSession().selectList(generateStatement("queryFilesByUserIdAndProfileId"), map);

        List<OrganFileResult> result = this.setResult(temp);

        return result;
    }

    @Override
    public List<OrganFileResult> queryFilesByProfileId(Long profileId) {
        Map<String, Object> map = new HashMap<>();
        map.put("profileId", profileId);
        List<Map<String, Object>> temp = getSqlSession().selectList(generateStatement("queryFilesByProfileId"), map);

        List<OrganFileResult> result = this.setResult(temp);

        return result;
    }

    @Override
    public List<OrganFileResult> queryPictures(Integer pageIndex, Integer pageSize, String mimeType) {
        PageView pageView = this.getPageView(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageView", pageView);
        map.put("mimeType", mimeType);
        List<Map<String, Object>> temp = getSqlSession().selectList(generateStatement("queryPictures"), map);

        List<OrganFileResult> result = this.setResult(temp);

        return result;
    }

    @Override
    public int queryPictureCount(String mimeType) {
        return this.getSqlSession().selectOne(generateStatement("queryPictureCount"), mimeType);
    }

    @Override
    public Long queryFilesCount() {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(OrgFile record) {
        return this.update(generateStatement("updateByPrimaryKeySelective"), record);
    }

    @Override
    public int updateByPrimaryKey(OrgFile record) {
        return 0;
    }

    @Override
    public int queryCountByFileName(String fileName, String mimeType) {
        Map<String, Object> map = new HashMap<>();
        map.put("fileName", fileName);
        map.put("mimeType", mimeType);

        return this.getSqlSession().selectOne(generateStatement("queryCountByFileName"), map);
    }

    @Override
    public int queryCountByUserId(Long userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);

        return this.getSqlSession().selectOne(generateStatement("queryCountByUserId"), map);
    }

    @Override
    public int queryCountByUserIdANdFileType(Long userId, String fileType) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("fileType", fileType);

        return this.getSqlSession().selectOne(generateStatement("queryCountByUserIdANdFileType"), map);
    }

    @Override
    public OrgFile selectByUserIdAndFileType(Long userId, String fileType,Long profileId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("fileType", fileType);
        map.put("profileId", profileId);

        return this.getObject(generateStatement("selectByUserIdAndFileType"), map);
    }

    @Override
    public OrgFile selectByUserIdAndFileTypeAndProfileId(Long userId, String fileType, Long profileId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("fileType", fileType);
        map.put("profileId", profileId);

        return this.getObject(generateStatement("selectByUserIdAndFileTypeAndProfileId"), map);
    }

    @Override
    public List<OrganFileResult> queryFilesByFileName(String fileName, Integer pageIndex, Integer pageSize, String mimeType) {
        PageView pageView = this.getPageView(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("pageView", pageView);
        map.put("fileName", fileName);
        map.put("mimeType", mimeType);

        List<Map<String, Object>> list = this.getSqlSession().selectList(generateStatement("queryFilesByFileName"), map);
        List<OrganFileResult> result = this.setResult(list);

        return result;
    }


    List<OrganFileResult> setResult(List<Map<String, Object>> list) {
        List<OrganFileResult> result = null;

        if (ListUtil.isNotEmpty(list)) {
            result = new ArrayList<>(list.size());

            for (Map<String, Object> item : list) {
                OrganFileResult model = new OrganFileResult();

                model.setId(MapUtils.getLong(item, "ID"));
                model.setFileName(MapUtils.getStr(item, "FILE_NAME"));
                model.setOriginFileName(MapUtils.getStr(item, "ORIGIN_FILE_NAME"));
                model.setHash(MapUtils.getStr(item, "HASH"));
                model.setFileUrl(MapUtils.getStr(item, "FILE_URL"));
                model.setMimeType(MapUtils.getStr(item, "MIME_TYPE"));
                model.setFileSize(MapUtils.getStr(item, "FILE_SIZE"));
                model.setState(MapUtils.getInteger(item, "STATE"));
                model.setCreateDate(MapUtils.getDate(item, "CREATE_DATE"));
                model.setUpdateDate(MapUtils.getDate(item, "UPDATE_DATE"));
                model.setDescription(MapUtils.getStr(item, "DESCRIPTION"));
                model.setBucket(MapUtils.getStr(item, "BUCKET_NAME"));
                model.setFileType(MapUtils.getStr(item, "FILE_TYPE"));
                model.setIsPublic(MapUtils.getBoolean(item, "IS_PUBLIC"));

                result.add(model);
            }
        }

        return result;
    }
}
